/**
 * @Project TMHSC
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Jan 30, 2011
 * @Createtime 11:27:26 PM
 */

package com.tmh.study.aASA
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;

	/**
	 * Use BitmapData.hitTest for non bitmaps
	 */
	public class BitmapCollision3 extends Sprite
	{
		private var bmpd1:BitmapData;

		private var bmpd2:BitmapData;

		private var star1:Star;

		private var star2:Star;

		public function BitmapCollision3()
		{
			// make 2 stars, add to stage
			star1 = new Star(50);
			addChild(star1);

			star2 = new Star(50);
			star2.x = 200;
			star2.y = 200;
			addChild(star2);

			// make 2 bitmap, not on stage
			bmpd1 = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0);
			bmpd2 = bmpd1.clone();

			stage.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
		}

		private function _mouseMoveHandler(event:MouseEvent):void
		{
			// move star 1 to the mouse position
			star1.x = mouseX;
			star1.y = mouseY;

			/**
			 * clear the bitmaps
			 *
			 * alpha channel is not specified, it take as zero
			 * so this has the result of making all pixels completely transparent
			 */
			bmpd1.fillRect(bmpd1.rect, 0);
			bmpd2.fillRect(bmpd2.rect, 0);

			// draw one star to each bitmap
			bmpd1.draw(star1, new Matrix(1, 0, 0, 1, star1.x, star1.y));
			bmpd2.draw(star2, new Matrix(1, 0, 0, 1, star2.x, star2.y));

			// the hit test itself
			if (bmpd1.hitTest(new Point(), 255, bmpd2, new Point(), 255))
			{
				star1.filters = [new GlowFilter()];
				star2.filters = [new GlowFilter()];
			}
			else
			{
				star1.filters = [];
				star2.filters = [];
			}
		}
	}
}